package com.tanhua.management.service;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.common.pojo.VideoInfo;
import com.tanhua.dubbo.server.pojo.Video;
import com.tanhua.management.api.VideoManage;
import com.tanhua.management.vo.VideoManageVo;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Create By Administrator on 2021/7/13.
 */
@Service
public class VideoManageService {

    @Reference(version = "1.0.0")
    private VideoManage videoManage;

    @Autowired
    private UserInfoService userInfoService;

//    @Reference(version = "1.0.0")
//    private VideoManageImp videoManageImp;



    public PageResult queryVideoManList(Integer page, Integer pageSize) {

        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pageSize);

        //查询videoinfo 表 获得所有的 数据
        List<VideoInfo> records = videoManage.queryVideoInfoList(page, pageSize).getRecords();

        //创建一个videoManageVo 用于封装
        VideoManageVo videoManageVo = new VideoManageVo();
        List<VideoManageVo> list = new ArrayList();
        //遍历 videoinfo  并获得 所有的 userid  及 video id
        for (VideoInfo record : records) {
            // 获得发布视频用户的id
            Long userId = record.getUserId();
            // 获得视频的id
            String videoId = record.getVideoId();
            //根据视频id 查询video 表
            Video video = videoManage.queryVideoByVideo(videoId);
            //如果为空直接返回
            if (ObjectUtil.isEmpty(video)) {
                return pageResult;
            }
            //给videoManageVo 封装数据
            videoManageVo.setId(video.getVid().intValue());
            videoManageVo.setUserId(userId.intValue());
            videoManageVo.setNickname(userInfoService.queryUserInfoByUserId(userId).getNickName());
            videoManageVo.setPicUrl(video.getPicUrl());
            videoManageVo.setVideoUrl(video.getVideoUrl());

            videoManageVo.setLikeCount(record.getLikeCount());
            videoManageVo.setCommentCount(record.getCommentCount());
            videoManageVo.setReportCount(record.getReportCount());
            videoManageVo.setLoveCount(record.getLoveCount());
            list.add(videoManageVo);
        }

        pageResult.setItems(list);

        return pageResult;
    }
}
